<template>
<!--  <el-select v-model="model" :placeholder="placeholder" clearable>-->
<!--    <el-option v-for="opt in opts" :key="opt.dictValue" :label="opt.dictTitle" :value="opt.dictValue"/>-->
<!--  </el-select>-->
  <el-tree-select style="width: 100%"
                  v-model="model" clearable
                  :data="departTree" default-expand-all
                  check-strictly node-key="id"
                  :props="defaultProps"
                  :render-after-expand="false"
  />
</template>
<script setup>
import {computed, defineProps, onMounted, ref, shallowRef, watchEffect} from 'vue';
import {DefaultFieldDefinition} from '@/components/form/DefaultFormDefinition';
import {handleLikeParam, isEmpty, listToTrees, real} from '@/utils/CommonUtils';
import {getAction} from '@/request/BaseRequest';
const departTree = shallowRef([]);

const defaultProps = {
  children: 'children',
  label: 'unitName',
};

onMounted(() => {
  loadDictOptions();
});

function loadDictOptions() {
  getAction('/sys/unit/loadDepatrsByCurrentUser').then(data => {
    if (data.success) {
      let result = data.result;
      setDepartOptions(result);
    }
  });
}

function setDepartOptions(result) {
  if (isEmpty(result)) result = [];
  result.forEach(d => {
    if (d.type + '' === '0') {
      d.disabled = true;
    } else {
      d.disabled = false;
    }
  });
  departTree.value = listToTrees(result);
}

const props = defineProps({
  field: {
    type: Object,
    default() {
      return DefaultFieldDefinition;
    }
  }
});
const model = shallowRef();

function resetField() {
  model.value = undefined;
}

function getSearchFieldValue(searchData) {
  if (isEmpty(model.value)) return;
  searchData.eq.push({
    fieldName: props.field.tableFieldName,
    value: model.value
  });
}

const placeholder = computed(() => {
  if (isEmpty(props.field.id)) return '';
  return `请选择${props.field.alias ? props.field.alias : props.field.showName}`;
});

defineExpose({
  getSearchFieldValue,
  resetField
});
</script>
